#!/bin/bash
# howto-cmake 36   # builds for 3.6
# howto-cmake 27 35 36 37 38 # builds for all those

set -e
set -x

# Prerequisites
# msys2 (See the msys2 file. Don't forget cmake)
# All the python.org pythons you want to be compatible with.
# ms-mpi (See the mingwnotes)
# Run each python (eg. e:/python38/python -i) and note the MSC version.
#  eg.
#    $ e:/python38/python -i
#    Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64
#    bit (AMD64)] on win32
#  In this case it is 1916
#  Then edit E:\Python38\Lib\distutils\cygwinccompiler.py and add before
#  the else:    raise ValueError("Unknown MS Compiler version...
#        elif msc_ver == '1916':
#            return ['msvcrt'] 
# Install visual c (needed to compile the cython generated files for
#  version 3 pythons).
# Install NSIS installer builder.

# in msys terminal in nrnwinobj
if test "x$OSTYPE" = "xmsys" ; then  #to end of file

SRC=$HOME/neuron/nrncmake
MARSHAL=/C/marshalnrn


# PYVS helps to construct setup.exe name. PYTHONS help with
# -DNRN_PYTHON_DYNAMIC argument.
PYVS="py"
PYTHONS=
for i in $* ; do
  PYVS=${PYVS}-$i
  PYTHONS="${PYTHONS}e:/python$i/python;"
done

export CYTHON=cython

cd $SRC
mkdir -p build
export NSRC=$SRC
ver="`sh nrnversion.sh 3`" # eg. 7.8
n1=nrn"${ver}"setup-AMD64.exe # name of setup.exe
#full distribution name
n2=nrn-"`git describe --dirty=+`".w64-mingw-${PYVS}-setup.exe

# building from scratch takes about 20 minutes on 2 core virtualbox guest
# when NRN_RX3D_OPT_LEVEL=0 (default). Distributions should have opt level 2.
#rm -r -f build/*
rm -r -f $MARSHAL
cd build

#rm -f src/mswin/$n2 # comment out to avoid build if it exists

if test ! -f src/mswin/$n2 ; then
  /mingw64/bin/cmake .. -G "Unix Makefiles" \
    -DCMAKE_INSTALL_PREFIX=$MARSHAL/nrn \
    -DPYTHON_EXECUTABLE=/e/python37/python \
    -DNRN_ENABLE_PYTHON_DYNAMIC=ON -DNRN_PYTHON_DYNAMIC="$PYTHONS" \
    -DNRN_ENABLE_MPI_DYNAMIC=ON \
    -DCMAKE_PREFIX_PATH="/c/ms-mpi" \
    -DNRN_RX3D_OPT_LEVEL=2 \
    -DNRN_ENABLE_CORENEURON=OFF

  make -j install
  rm -f src/mswin/$n1
  make setup_exe
  mv src/mswin/$n1 src/mswin/$n2
fi

echo "scp src/mswin/$n2  hines@neuron.yale.edu:/home/htdocs/ftp/neuron/versions/alpha/$n2"

h=hines@10.10.0.2
scp src/mswin/$n2 $h:vmshared/$n2
if true ; then
ssh $h scp vmshared/$n2 hines@neuron.yale.edu:/home/htdocs/ftp/neuron/versions/alpha/$n2
fi

fi #MSYS

